package cn.larry.tools.md5;

import cn.larry.tools.cons.Constants;
import cn.larry.tools.string.StringTool;

import java.security.MessageDigest;

/**
 * @version 1.0
 * @Author llin
 * @Date 2020/5/28 14:12
 */
public class Md5Tool implements Md5 {

    /**
     * 16位的MD5加密
     *
     * @param str
     * @param charsetName 编码格式（default：UTF-8）
     * @return
     */
    @Override
    public String encrypt16(String str, String charsetName) {
        try {
            return encrypt32(str, charsetName).substring(8, 24);
        } catch (Exception e) {
            throw new RuntimeException(
                    "[encrypt16]Error initializing. Cause: " + e);
        }
    }

    /**
     * 16位的MD5加密
     *
     * @param strByte
     * @return
     */
    @Override
    public String encrypt16(byte[] strByte) {
        try {
            return encrypt32(strByte).substring(8, 24);
        } catch (Exception e) {
            throw new RuntimeException(
                    "[encrypt16]Error initializing. Cause: " + e);
        }
    }

    /**
     * 16位的MD5加密（大写）
     *
     * @param str
     * @param charsetName
     * @return
     */
    @Override
    public String encrypt16ToUpperCase16(String str, String charsetName) {
        return encrypt16(str, charsetName).toUpperCase();
    }

    /**
     * 16位的MD5加密（大写）
     *
     * @param strByte
     * @return
     */
    @Override
    public String encrypt16ToUpperCase(byte[] strByte) {
        return encrypt16(strByte).toUpperCase();
    }

    /**
     * 32位的MD5加密
     *
     * @param str
     * @param charsetName 编码格式（default：UTF-8）
     * @return
     */
    @Override
    public String encrypt32(String str, String charsetName) {
        if (StringTool.isBlank(charsetName))
            charsetName = Constants.CODING_NAME_UTF8_UPCASE;

        String result = "";
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(str.getBytes(charsetName));
            byte s[] = m.digest();
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "[encrypt32]Error initializing. Cause: " + e);
        }
        return result;
    }

    /**
     * 32位的MD5加密
     *
     * @param strByte
     * @return
     */
    @Override
    public String encrypt32(byte[] strByte) {

        String result = "";
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(strByte);
            byte s[] = m.digest();
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "[encrypt32]Error initializing. Cause: " + e);
        }
        return result;
    }

    /**
     * 32位的MD5加密（大写）
     *
     * @param str
     * @param charsetName 编码格式（default：UTF-8）
     * @return
     */
    @Override
    public String encrypt32ToUpperCase32(String str, String charsetName) {
        return encrypt32(str, charsetName).toUpperCase();
    }

    /**
     * 32位的MD5加密（大写）
     *
     * @param strByte
     * @return
     */
    @Override
    public String encrypt32ToUpperCase(byte[] strByte) {
        return encrypt32(strByte).toUpperCase();
    }


    /**
     * 通过盐值对字符串进行MD5加密
     *
     * @param param 需要加密的字符串
     * @param salt  盐值
     * @return
     */
    public static String MD5(String param, String salt) {
        return MD5(param + salt);
    }

    /**
     * 加密字符串
     *
     * @param s 字符串
     * @return
     */
    public static String MD5(String s) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
}
